// 本文件方法用于升级数据库表
// 如果有表结构的变化请用此文件来升级
// 例1：增加表字段
/*
knex.schema.hasColumn(TABLE_NAME.worker, 'proposalStatus').then((exist) => {
  if (!exist) {
    knex.schema.alterTable(TABLE_NAME.worker, (t) => {
      t.string('proposalStatus');
    });
  }
});
*/

// 例2：修改表字段类型
/*
knex.schema.alterTable(TABLE_NAME.contract, (t) => {
  t.timestamp('contractEndTime').defaultTo(knex.fn.now()).alter();
});
*/

// 例3：增加或修改外键
/*
knex.schema.alterTable(TABLE_NAME.contract, (t) => {
  t.dropForeign('tenantId');
  t.foreign('tenantId').references(`${TABLE_NAME.tenant}.id`)
    .onDelete('CASCADE');
});
*/

const { knex, TABLE_NAME } = require('./db');

module.exports = async () => {
  if (!await knex.schema.hasColumn(TABLE_NAME.worker, 'deadline')) {
    knex.schema.alterTable(TABLE_NAME.worker, (t) => {
      t.string('deadline');
    });
  }
  if (!await knex.schema.hasColumn(TABLE_NAME.worker, 'proposalStatus')) {
    knex.schema.alterTable(TABLE_NAME.worker, (t) => {
      t.string('proposalStatus');
    }).catch((error)=>{
      console.log("worker  alter proposalStatus:",error);
    });
  }
  if (!await knex.schema.hasColumn(TABLE_NAME.contract, 'type')) {//工人合同类型
    knex.schema.alterTable(TABLE_NAME.contract, (t) => {
      t.string('type');
    }).catch((error)=>{
      console.log("contract  alter type:",error);
    });
  }
  //workerId
  if (!await knex.schema.hasColumn(TABLE_NAME.workerSignRecord, 'workerId')) {//工人id
    knex.schema.alterTable(TABLE_NAME.workerSignRecord, (t) => {
      t.string('workerId');
    }).catch((error)=>{
      console.log("workerSignRecord  alter type:",error);
    });
  }
  if (!await knex.schema.hasColumn(TABLE_NAME.send, 'workerId')) {//工人id
    knex.schema.alterTable(TABLE_NAME.send, (t) => {
      t.string('workerId');
    }).catch((error)=>{
      console.log("send  alter type:",error);
    });
  }
};
